﻿using Quartz;
using Quartz.Impl;

namespace MvcForum.Net.Bussiness.Jobs
{
    public class MvcForumScheduler
    {

        private static IScheduler _scheduler;

        public static IScheduler Instant { get { return _scheduler; } }


        protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MvcForumScheduler));

        public static void Start()
        {
            ISchedulerFactory sf = new StdSchedulerFactory();
            _scheduler = sf.GetScheduler();
            BuildJobs(_scheduler);

            log.Info("Quartz.Net Scheduler Start");
            _scheduler.Start();
        }

        private static void BuildJobs(IScheduler _sched)
        {

            log.Info("Build Email Jobs");

            IJobDetail job = JobBuilder.Create<SendEmail>()
               .WithIdentity("job1", "group1")
               .Build();

            var trigger = (ICronTrigger)TriggerBuilder.Create()
                                                      .WithIdentity("trigger1", "group1")
                                                      .WithCronSchedule("0 0/2 * * * ?")
                                                      .Build();

            _sched.ScheduleJob(job, trigger);

            log.Info("Build Email Jobs Completed");
        }

    }
}
